package com.alibaba.org.service.impl;

import com.alibaba.basic.exception.BusinessException;
import com.alibaba.basic.service.impl.BaseServiceImpl;
import com.alibaba.basic.util.BaiduAuditUtils;
import com.alibaba.basic.util.Md5Utils;
import com.alibaba.basic.util.StrUtils;
import com.alibaba.org.domain.Employee;
import com.alibaba.org.domain.Shop;
import com.alibaba.org.domain.ShopAuditLog;
import com.alibaba.org.dto.ShopDto;
import com.alibaba.org.mapper.EmployeeMapper;
import com.alibaba.org.mapper.ShopAuditLogMapper;
import com.alibaba.org.mapper.ShopMapper;
import com.alibaba.org.service.IShopService;
import com.alibaba.user.domain.Logininfo;
import com.alibaba.user.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
   private JavaMailSender javaMailSender;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void settlement(Shop shop) {
        //一：检验 - 空值校验：前端的js校验容易被禁用，不安全，后端也要做校验
        if(StringUtils.isEmpty(shop.getName())){ //空和空字符串返回true
            throw new BusinessException("提交资料不能为空!!!");
        }
        // 检验店铺名称是否合法
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名字不合法请重新输入!!!");
        }
        // 检验logo
        if (!StringUtils.isEmpty(shop.getLogo())){
            if (BaiduAuditUtils.ImgCensor("http://123.207.27.208" +shop.getLogo())){
            throw new BusinessException("图片不合法!!!");
            }
        }
        //二：校验 - 当前店铺是否入驻过
        Shop dbShop = shopMapper.findByNameAndAddress(shop);
        if(dbShop != null){//查到了 = 入驻过
            throw new BusinessException("请勿重复入驻，忘记密码请联系管理员!!!");
        }

        //添加logininfo信息
        Employee admin = shop.getAdmin();
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setSalt(salt);
        admin.setPassword(md5Pwd);

        //添加logininfo信息
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);//可以登陆
        logininfoMapper.add(logininfo);

        //三：数据库操作
        //1.保存t_employee员工信息 - 生成自增id
        admin.setLogininfoId(logininfo.getId());
        employeeMapper.add(admin);//只要添加之后，就生成了自增id

        //2.将自增id设置到shop对象，并保存店铺信息 - 生成店铺的自增id
        shop.setAdminId(admin.getId());
        shopMapper.add(shop);

        //3.将店铺的shopid设置到employee对象，修改employee信息
        admin.setShopId(shop.getId()); //admin对象：id，shopId
        employeeMapper.update(admin);

    }

    @Override
    public void pass(ShopAuditLog log) throws MessagingException {
        // 修改状态， 添加日志 ，发邮件
        Shop shop = shopMapper.findById(log.getShopId());
        shop.setState(2);
        shopMapper.update(shop);
        // 保存到日志
        log.setState(2);
        // 审核人
        log.setAuditId(111l);
        shopAuditLogMapper.add(log);
        // 发邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // true-能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("3066499039@qq.com");
        helper.setSubject("店铺激活邮件");


        // true-能够在内容上添加标签 能被解析
        helper.setText("你的店铺已经通过审核请<a herf='http://localhost:8080/shop/active/"+log.getShopId()+"'>点击这里</a> 激活邮件",true);
        // 添加附件
        helper.addAttachment("p82.jpg",new File("D:\\sofeware\\resources\\p82.jpg"));
        // 获取管理人员的邮箱
        Employee admin = employeeMapper.findById(shop.getAdminId());

        helper.setTo(admin.getEmail());
        javaMailSender.send(mimeMessage);

    }

    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        // 修改状态， 添加日志 ，发邮件
        Shop shop = shopMapper.findById(log.getShopId());
        shop.setState(4);
        shopMapper.update(shop);
        // 保存到日志
        log.setState(4);
        // 审核人
        log.setAuditId(111l);
        shopAuditLogMapper.add(log);
        // 发邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // true-能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("3066499039@qq.com");
        helper.setSubject("店铺激活失败邮件");


        // true-能够在内容上添加标签 能被解析
        helper.setText("你的店铺审核未通过<a herf='http://localhost:8081/#/registerEdit/"+log.getShopId()+"'>点击这里</a> 重新修改信息",true);
        // 添加附件
        helper.addAttachment("p82.jpg",new File("D:\\sofeware\\resources\\p82.jpg"));
        // 获取管理人员的邮箱
        Employee admin = employeeMapper.findById(shop.getAdminId());

        helper.setTo(admin.getEmail());
        javaMailSender.send(mimeMessage);
    }
    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }


}
